// 在Cloud code里初始化express框架
var express = require('express');
var avosExpressHttpsRedirect = require('avos-express-https-redirect');
var app = express();
var expressLayouts = require('express-ejs-layouts');//使用layouts

var OAuth = require('cloud/OAuth.js');

// App全局配置
app.set('views','cloud/views');   //设置模板目录
app.set('view engine', 'ejs');    // 设置template引擎
app.use(express.bodyParser());    // 读取请求body的中间件
app.use(avosExpressHttpsRedirect()); //启用HTTPS
app.use(expressLayouts);


/************************************User******************************/
var OAuth = OAuth.OAuth;
var test = new OAuth('wx855d23517352ad96','http://forehead.avosapps.com/code','21198a4de5d5912d0b390363cdbcb31c',"code","STATE",app);
var testurl;

function wxTest(res,req){
	var currentUser = AV.User.current();

	if (currentUser) {
	  // do stuff with the user

	} else {
	  // show the signup or login page
	  console.log(testurl);
		test.getCode(req,res);
	}

	var query = new AV.Query(GameScore);
	query.equalTo("playerEmail", "dstemkoski@example.com");
	query.first({
	  success: function(object) {
	    // Successfully retrieved the object.
	  },
	  error: function(error) {
	    alert("Error: " + error.code + " " + error.message);
	  }
	});
}

app.get('/getUserInfo',function(req,res){
  test.getCode(req,res);
})
app.get('/code',function(req,res){
  var code = req.query.code;
  test.getUserInfo(code,req,res,testurl);
})



/**********************************get请求****************************/
app.get('/index', function(req, res){
	wxTest(res,req);
	testurl = "index";
	// res.render("index");
});
app.get('/', function(req, res){
	wxTest(res,req);
	testurl = "index";
	// res.render("index");
});
app.get('/dealInterview', function(req, res){
	res.render("dealInterview");
});
app.get('/recommend', function(req, res){
	res.render("recommend");
});
app.get('/recommendDetail', function(req, res){
	res.render("recommendDetail");
});
app.get('/resumeDetail', function(req, res){
	res.render("resumeDetail");
});
app.get('/reward', function(req, res){
	res.render("reward");
});
app.get('/timeUp', function(req, res){
	res.render("timeUp");
});
app.get('/try', function(req, res){
	res.render("try");
});
app.get('/usePeople', function(req, res){
	res.render("usePeople");
});
app.get('/user', function(req, res){
	res.render("user");
});
app.get('/userInfo', function(req, res){
	res.render("userInfo");
});
app.get('/userMessage', function(req, res){
	res.render("userMessage");
});

/********************************test用户系统*************************************/

// test.getCode();
// app.get('/getUserInfo',function(req,res){
//   test.getCode(req,res);
// })


/**********************************************************************/

//最后，必须有这行代码来使express响应http请求
app.listen();
